package com.jianshuge.app.widget;

import java.io.Serializable;
import java.util.HashMap;  
import java.util.Iterator;
import java.util.List;  
import java.util.Map;  

import android.content.Context;  
import android.content.Intent;  
import android.graphics.Color;
import android.text.SpannableStringBuilder;  
import android.text.Spanned;  
import android.text.TextPaint;
import android.text.TextUtils;  
import android.text.method.LinkMovementMethod;  
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.EditText;  
import android.widget.TextView;  


/** 
 * TextView with intent that can redirect to a new activity 
 */  
public class CustomTextView {  
  
    private static Map<String, String> faceMap;  
  
    static {  
        faceMap = new HashMap<String, String>();  
        faceMap.put("[哭]", "face_1");  
        faceMap.put("[怒]", "face_2");  
    }  
  
    
	public static class divText {
		public String  text;
		public boolean is_mark;
	}
	

    public static void setClickableTextView(Context context, TextView textView, List<divText> textList ) {  
        if (textList == null ) {  
            return;  
        }  
        SpannableStringBuilder builder = new SpannableStringBuilder();  
        int end = -1, length = -1;  
        int size = textList.size();  
        for (int i = 0; i < size; i++) {  
        	divText div_text = textList.get(i);  
            
            if (TextUtils.isEmpty(div_text.text)) {  
                continue;  
            }
            
            builder.append(div_text.text);  
            if ( div_text.is_mark == true ) {  
                end = builder.length();  
                length = div_text.text.length();  
                builder.setSpan(new NoClickableSpan( div_text.text ), end - length, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
            }  
            builder.append(" "); 
        }  
        textView.setText(builder);  
        textView.setFocusable(true);  
        textView.setMovementMethod(LinkMovementMethod.getInstance());  
    }  
    /** 
     * make textview a clickable textview<br> 
     * Note: make true the order of textList and intentList are mapped 
     *  
     * @param context 
     * @param textView 
     * @param textList 
     *            the text should be set to this textview,not null 
     * @param intentList 
     *            the intent map to the text, if the text have no intent mapped 
     *            to, please set a null value.Or it will happen some unknown 
     *            error.<br> 
     *            not null 
     */  
    public static void setClickableTextView(Context context, TextView textView,  
            List<String> textList, List<Intent> intentList) {  
        if (textList == null || intentList == null) {  
            return;  
        }  
        SpannableStringBuilder builder = new SpannableStringBuilder();  
        int end = -1, length = -1;  
        int size = textList.size();  
        Intent intent;  
        for (int i = 0; i < size; i++) {  
            String text = textList.get(i);  
            if (TextUtils.isEmpty(text)) {  
                continue;  
            }  
            builder.append(textList.get(i));  
            if ((intent = intentList.get(i)) != null) {  
                end = builder.length();  
                length = textList.get(i).length();  
                builder.setSpan(getClickableSpan(context, intent),  
                        end - length, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
            }  
            builder.append(" ");  
        }  
        textView.setText(builder);  
        textView.setFocusable(true);  
        textView.setMovementMethod(LinkMovementMethod.getInstance());  
    }  
      
    /** 
     *  make textview a clickable textview<br> 
     *  Note: make true the order of textList and intentList are mapped 
     * @param context 
     * @param textView 
     * @param text 
     * @param intent 
     */  
    public static void setClickableTextView(Context context, TextView textView,  
            String text, Intent intent) {  
        SpannableStringBuilder builder = new SpannableStringBuilder(text);  
        builder.setSpan(getClickableSpan(context, intent), 0, text.length(),   
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
        textView.setText(builder);  
        textView.setMovementMethod(LinkMovementMethod.getInstance());  
    }  
  
    /** 
     * make TextView a View with image at any index   
     * @param context 
     * @param textView 
     * @param textList 
     */  
    public static void setImgTextView(Context context, TextView textView,  
            List<String> textList) {  
        StringBuilder builder = new StringBuilder();  
        for (int i = 0; i < textList.size(); i++) {  
            builder.append(textList.get(i)).append(" ");  
        }  
        setImgTextView(context, textView, builder.toString());  
  
    }  
  
    /** 
     * make TextView a View with image at any index   
     * @param context 
     * @param textView 
     * @param text 
     */  
    public static void setImgTextView(Context context, TextView textView,  
            String text) {  
        ImageSpan imageSpan = new ImageSpan(context, faceMap);  
        Spanned spanned = imageSpan.getImageSpan(text);  
        textView.setText(spanned);  
    }  
      
    /** 
     * make EditText a View with image at any index   
     * @param context 
     * @param EditText 
     * @param text 
     */  
    public static void setImgTextView(Context context, EditText editText,  
            String text) {  
        ImageSpan imageSpan = new ImageSpan(context, faceMap);  
        Spanned spanned = imageSpan.getImageSpan(text);  
        editText.setText(spanned);  
    }  
  
    /** 
     * return a custom ClickableSpan 
     *  
     * @param context 
     * @param intent 
     * @return 
     */  
    public static MyClickableSpan getClickableSpan(Context context,  
            Intent intent) {  
        return new MyClickableSpan(context, intent);  
    }  
  
    /** 
     * make textview a clickable textview with image<br> 
     * Note: make true the order of textList and intentList are mapped 
     *  
     * @param context 
     *            not null 
     * @param haveImg 
     *            whether this is image in the text,not null 
     * @param textView 
     *            not null 
     * @param textList 
     *            the text should be set to this textview,not null 
     * @param intentList 
     *            the intent map to the text, if the text have no intent mapped 
     *            to, please set a null value.Or it will happen some unknown 
     *            error.<br> 
     *            allow null 
     */  
    public static void setCustomText(Context context, Boolean haveImg,  
            TextView textView, List<String> textList, List<Intent> intentList) {  
        SpannableStringBuilder builder = new SpannableStringBuilder();  
        int end = -1, length = -1;  
        if (intentList != null) {  
            int size = textList.size();  
            Intent intent;  
            for (int i = 0; i < size; i++) {  
                String text = textList.get(i);  
                if (TextUtils.isEmpty(text)) {  
                    continue;  
                }  
                builder.append(textList.get(i));  
                if ((intent = intentList.get(i)) != null) {  
                    end = builder.length();  
                    length = textList.get(i).length();  
                    builder.setSpan(getClickableSpan(context, intent), end  
                            - length, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
                }  
                builder.append(" ");  
            }  
        } else {  
            for (String text : textList) {  
                builder.append(text).append(" ");  
            }  
        }  
        if (haveImg) {  
            ImageSpan imageSpan = new ImageSpan(context, faceMap);  
            Spanned spanned = imageSpan.getImageSpan(builder);  
            textView.setText(spanned);  
        } else {  
            textView.setText(builder);  
        }  
        textView.setMovementMethod(LinkMovementMethod.getInstance());  
  
    }  
  
}  